<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 200px;
        border: 1px solid red;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <button id="start">开始</button>
    <button id="end">结束</button>

    <script>
      //随机抽取六位数
      function color() {
        var str = '0123456789abcdef';
        var rgb = '#';
        for (var i = 0; i < 6; i++) {
          rgb += str[nunEle(0, str.length - 1)];
        }
        return rgb;
      }
      //封装随机函数
      function nunEle(min, max) {
        return Math.round(Math.random() * (max - min)) + min;
      }
      console.log(nunEle(0, 15));

      var startEle = document.getElementById('start');
      var endEle = document.getElementById('end');
      var boxEle = document.getElementById('box');
      var timer;
      startEle.onclick = function () {
        if (timer) {
          console.log(timer);
          return;
        }
         timer = setInterval(function () {
          boxEle.style.backgroundColor = color();
        }, 1000);
      };
      endEle.onclick = function () {
          clearInterval(timer);
          timer = undefined;
        };
    </script>
  </body>
</html>
